#!/usr/bin/env python
# coding: utf-8
 
import cv2
import socket
import numpy as np
 
def main():
    img_path = r"/opt/MVS/Samples/aarch64/Python/hksdkdemo-python-aarch64/Samples/aarch64/Python/GrabImage/frame.jpg"
 
    ip = '10.48.0.4'
    port = 8084
 
    # 建立socket客户端，连接主机
    sock = socket.socket()
    sock.connect((ip, port))
 
    # 图像二进制编码
    img = cv2.imread(img_path)
    _, img_encoded = cv2.imencode('.jpg', img)  # 编码为 JPEG 格式
    img_data = img_encoded.tobytes()  
 
    # 发送
    # sock.send(str(len(str_data)).ljust(16).encode())
    # sock.send(str_data)
    # sock.close()
     # 发送图像数据
    sock.sendall(img_data)  # 使用 sendall 发送完整数据
    sock.close()

 
    # # 解码为图像，显示发送的二进制流
    # decode_img = cv2.imdecode(data, 1)
    # cv2.imshow('client', decode_img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
 
if __name__ == "__main__":
    main()
    pass




# import socket

# # B 服务器的 IP 和端口
# server_ip = '10.48.0.4'  # B 服务器的 IP 地址
# server_port = 8084          # B 服务器的端口

# # 创建一个 TCP 套接字
# client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# try:
#     # 连接到 B 服务器
#     client_socket.connect((server_ip, server_port))
#     print("Connected to the server.")

#     # 发送数据
#     message = b'Hello, Server!'
#     client_socket.sendall(message)
#     print("Message sent to the server.")

# finally:
#     # 关闭客户端连接
#     client_socket.close()
